package com.woniu.zsd.waste.service.impl;


import com.woniu.commons.util.RedisKey;
import com.woniu.zsd.commons.exception.ZsdException;
import com.woniu.zsd.waste.entity.WasteType;
import com.woniu.zsd.waste.mapper.WasteTypeMapper;
import com.woniu.zsd.waste.service.IWasteTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import zsd.waste.form.AddWasteTypeForm;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@Service
public class WasteTypeServiceImpl extends ServiceImpl<WasteTypeMapper, WasteType> implements IWasteTypeService {

    @Autowired
    private WasteTypeMapper wasteTypeMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public List<WasteType> selWasteType() {
        List<WasteType> wasteType = wasteTypeMapper.selWasteType();
        for (WasteType type : wasteType) {
            redisTemplate.opsForHash().put(RedisKey.WASTE_TYPE_KEY(),(type.getId()).toString(),type.getWasteTypeName());
        }
        return wasteType;
    }

    @Override
    public int insertWasteType(AddWasteTypeForm awtf) {
        // 检查废品类型名称是否已存在
        int count = wasteTypeMapper.countByWasteTypeName(awtf.getWasteTypeName());
        if (count > 0) {
            throw new ZsdException("废品类型名称已存在", 500);
        }

        // 执行插入操作
        return wasteTypeMapper.insertWasteType(awtf);
    }

    @Override
    public int updateWasteType(WasteType wasteType) {;
        int result = wasteTypeMapper.updateWasteType(wasteType);

        // 将更新后的数据同步到Redis
        WasteType updatedWasteType = wasteTypeMapper.getWasteTypeById(wasteType.getId());
        if (updatedWasteType != null) {
            redisTemplate.opsForHash().put(RedisKey.WASTE_TYPE_KEY(), updatedWasteType.getId().toString(), updatedWasteType.getWasteTypeName());
        }

        return result;}

    @Override
    public int deleteWasteType(Integer id) {
        // 从Redis中删除对应的数据
        redisTemplate.opsForHash().delete(RedisKey.WASTE_TYPE_KEY(), id.toString());

        // 执行删除操作
        return wasteTypeMapper.deleteWasteType(id);}

    @Override
    public WasteType getWasteTypeById(Integer id) {
        return wasteTypeMapper.getWasteTypeById(id);
    }

    @Override
    public WasteType getWasteTypeByName(String name) {
        // 查询废品类型
        return wasteTypeMapper.getWasteTypeByName(name);
    }

    @Override
    public List<WasteType> getTop5SearchedWasteTypes() {
        String key = RedisKey.WASTE_TYPE_COUNT_KEY();
        // 获取查询次数最多的五个废品类型名称
        Set<String> top5Names = redisTemplate.opsForZSet().reverseRange(key, 0, 4);

        // 根据名称查询废品类型详情
        List<WasteType> top5WasteTypes = new ArrayList<>();
        if (top5Names != null) {
            for (String name : top5Names) {
                WasteType wasteType = wasteTypeMapper.getWasteTypeByName(name);
                if (wasteType != null) {
                    top5WasteTypes.add(wasteType);
                }
            }
        }

        return top5WasteTypes;
    }
}